package com.dwms.controller;

import com.dwms.entity.ListRequest;
import com.dwms.entity.PageResponse;
import com.dwms.entity.QueryRequest;
import com.dwms.entity.Result;
import com.dwms.entity.declare.jeInbound.request.JeInboundImportRequest;
import com.dwms.entity.declare.jeOutbound.request.JeOutboundImportRequest;
import com.dwms.entity.declare.jeOutbound.request.JeOutboundQueryRequest;
import com.dwms.entity.declare.jeOutbound.request.JeOutboundRequest;
import com.dwms.entity.declare.jeOutbound.response.JeOutboundResponse;
import com.dwms.feign.declare.JeOutboundFeignClient;
import com.dwms.utils.EmptyUtils;
import com.dwms.utils.ExcelImportUtil;
import com.dwms.utils.WebUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName: JeOutboundController
 * @Description: JeOutbound
 * @Create 2025-09-28 20:48:45
 * @Author WH
 * @Since 1.0.0
 **/
@Tag(name = "金二账册出库")
@RestController
@RequestMapping(value = "/declare/jeOutbound")
@RequiredArgsConstructor
public class JeOutboundController {

    private final HttpServletRequest request;
    private final JeOutboundFeignClient jeOutboundFeignClient;


    /**
     * 分页查询
     *
     * @param query 查询参数
     * @return
     */
    @Operation(summary = "分页查询")
    @PostMapping("/findPage")
    public Result<PageResponse<JeOutboundResponse>> findPage(@RequestBody JeOutboundQueryRequest query) {
        WebUtil.getAdminRequest(request, query);
        return jeOutboundFeignClient.findPage(query);

    }

    /**
     * 根据ID查询
     *
     * @param query 参数
     * @return
     */
    @Operation(summary = "根据ID查询")
    @PostMapping("/findById")
    public Result findById(@RequestBody QueryRequest query) {
        WebUtil.getAdminRequest(request, query);
        return this.jeOutboundFeignClient.findById(query);
    }

    /**
     * 出库
     *
     * @param jeOutboundRequest
     *          请求参数
     * @return
     */
    @Operation(summary = "出库")
    @PostMapping("/outbound")
    public Result update(@RequestBody JeOutboundRequest jeOutboundRequest) {
        WebUtil.getAdminRequest(request, jeOutboundRequest);
        return this.jeOutboundFeignClient.update(jeOutboundRequest);
    }

    /**
     * 删除
     *
     * @param query 参数
     * @return
     */
    @Operation(summary = "删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody QueryRequest query) {
        WebUtil.getAdminRequest(request, query);
        return this.jeOutboundFeignClient.delete(query);
    }

    /**
     * 出区导入
     *
     * @param file
     * @return
     */
    @PostMapping("/importExcel")
    public Result importExcel(@RequestParam(name = "file") MultipartFile file) {
        try {
            String[] headers = {"电商企业编码", "运单号"};
            String[] fields = {"ebcCode", "waybill"};
            List<JeOutboundImportRequest> jeOutboundImportRequests = ExcelImportUtil.excelToBeanUnderHead(file.getInputStream(), headers, fields, JeOutboundImportRequest.class);
            if (EmptyUtils.isEmpty(jeOutboundImportRequests)) {
                return Result.failed("数据读取失败");
            }
            if (jeOutboundImportRequests.size() > 10000) {
                return Result.failed("导入数据超过10000条,请分批次导入.");
            }
            ListRequest<JeOutboundImportRequest> listRequest = new ListRequest<>();
            listRequest.setList(jeOutboundImportRequests);
            WebUtil.getAdminRequest(request, listRequest);
            return this.jeOutboundFeignClient.importExcel(listRequest);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("导入出区数据失败" + e.getMessage());
        }
    }

    /**
     * 导出清单
     *
     * @param query
     * @return
     */
    @PostMapping("/exportInvt")
    public Result<String> exportInvt(@RequestBody QueryRequest query) {
        WebUtil.getAdminRequest(request, query);
        return jeOutboundFeignClient.exportInvt(query);
    }

    /**
     * 导出清单明细
     *
     * @param query
     * @return
     */
    @PostMapping("/exportInvtDetail")
    private Result<String> exportInvtDetail(@RequestBody QueryRequest query) {
        WebUtil.getAdminRequest(request, query);
        return jeOutboundFeignClient.exportInvtDetail(query);
    }

    /**
     * 导出盘点清单
     *
     * @param query
     * @return
     */
    @PostMapping("/exportCheckInvt")
    public Result<String> exportCheckInvt(@RequestBody QueryRequest query) {
        WebUtil.getAdminRequest(request, query);
        return jeOutboundFeignClient.exportCheckInvt(query);
    }
}
